package com.ruoyi.generator.service;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.generator.entity.*;
import com.ruoyi.generator.entity.bo.MaintenancPlaneBO;
import com.ruoyi.generator.entity.bo.PlanScheduleCountBo;
import com.ruoyi.generator.mapper.InspectionMapper;
import com.ruoyi.generator.mapper.MaintenanceMapper;
import com.ruoyi.generator.po.*;
import com.ruoyi.generator.vo.MaintenanceAppVO;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 业务字段 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class MaintenanceServiceImpl implements MaintenanceService
{
	@Autowired
	private MaintenanceMapper maintenanceMapper;


	@Override
	public List<MaintenanceScheduleShift> selectScheduleList() {

		List<MaintenanceWushuiShift> WushuiShift =maintenanceMapper.selectMaintenanceScheduleList();
		return convertToScheduleShifts(WushuiShift);
	}

	@Override
	public List<MaintenanceAppVO> getMaintenanceListByStatus(MaintenanceAppVO m) {
		if(m.getMaintenanceStatus().equals("0")){
			m.setRemark("4");
		}
		List<MaintenanceAppVO> MainList =maintenanceMapper.getMaintenanceListByStatus(m);
		return MainList;
	}

	@Override
	public List<MaintenancePlan> selectMaintenancePlanList(Maintenance Maintenance) {
		List<MaintenancePlan> maintenancePlanList =maintenanceMapper.selectMaintenancePlanList(Maintenance);
		return maintenancePlanList;
	}
	@Override
	public List<ClUser> selectByrs(ClUser cl) {
		List<ClUser> clList =maintenanceMapper.selectByrs(cl);
		return clList;
	}



	@Override
	public List<PlanScheduleCountBo> selectMainScheduleCount() {
		List<PlanScheduleCountBo> countList =maintenanceMapper.selectMainScheduleCount();
		return countList;

	}

	@Override
	public List<WushuiSchedule> getMaintenanceSchedules() {
		List<WushuiSchedule> wushuiSchedules = maintenanceMapper.getMaintenanceSchedules();
		return wushuiSchedules;
	}

	@Override
	public int addMaintenancePlan(MaintenancPlaneBO maintenancPlaneBO) {
		MaintenancePlan maintenancePlan = new MaintenancePlan();
		for (String maintenanceId : maintenancPlaneBO.getMaintenanceId()){
			maintenancePlan.setMaintenanceId(maintenanceId);
			maintenancePlan.setUserId(maintenancPlaneBO.getUserId());
			maintenancePlan.setStartDate(LocalDate.now());  // 新增：设置开始时间为当前系统时间
			maintenancePlan.setMaintenanceStatus("0");
			maintenancePlan.setMaintenancePeriod(String.valueOf(maintenancPlaneBO.getScheduleId()));
			maintenanceMapper.updatePlan(maintenancePlan);
		}
		return  12;
	}

	public List<MaintenanceScheduleShift> convertToScheduleShifts(List<MaintenanceWushuiShift> wushuiShifts) {
		List<MaintenanceScheduleShift> scheduleShifts = new ArrayList<>();

		// 按照 period 进行分组
		for (MaintenanceWushuiShift shift : wushuiShifts) {
			String period = shift.getPeriod();
			MaintenanceScheduleShift scheduleShift = findOrCreateScheduleShift(scheduleShifts, period);
			scheduleShift.getShifts().add(shift);
		}

		return scheduleShifts;
	}
	private MaintenanceScheduleShift findOrCreateScheduleShift(List<MaintenanceScheduleShift> scheduleShifts, String period) {
		for (MaintenanceScheduleShift scheduleShift : scheduleShifts) {
			if (scheduleShift.getPeriod().equals(period)) {
				return scheduleShift;
			}
		}
		// 创建新的 MaintenanceScheduleShift
		MaintenanceScheduleShift newScheduleShift = new MaintenanceScheduleShift();
		newScheduleShift.setPeriod(period);
		newScheduleShift.setShifts(new ArrayList<>());
		scheduleShifts.add(newScheduleShift);
		return newScheduleShift;
	}
}
